var ns, actionsheet, toast, actionsheet, artEditor, map,UIAlbumBrowser;
var app = new Vue({
    el: '#app',
    data: {
        categorylist: [],//举报类型
        form: {
            RtypeID: 0,//选中的类型id
            ReportedUserID:0,//被举报人id
            ThreadID:0,//举报帖子id
            ProjectID:0,//项目id
            ReportType:0,//1--举报人，2--举报帖子
            Reasons:'',
            Type:0,//1-拉黑，2-举报
            action:'savamallreporttype'
        },
        
        list: [],
        imglist: [],
        imgindex: 0,
        img_count: 0,
        select_imglist: [],
        select_imgindex: 0,
        maxuploadcount: CONFIG.maxuploadcount,
    },
    methods: {
        get_data: function() {
            var that = this;
            ns.post({
                action: 'getmallreporttype',
            }, function(succeed, data, err) {
                console.log(JSON.stringify(data));
                if (succeed) {
                    that.categorylist = data.Type;
                }
            });
        },
        do_tag_select: function(item) {
            var that = this;
            for (var i = 0; i < that.categorylist.length; i++) {
                that.categorylist[i].IsCheck = false;
            }
            item.IsCheck = true;
            that.form.RtypeID = item.ID;
        },
        do_reset: function() {
            var that = this;
            for (var i = 0; i < that.categorylist.length; i++) {
                that.categorylist[i].IsCheck = false;
            }
            that.form.RtypeID = 0;
        },
        do_save: function() {
            var that = this;
            var filelist = [];
            for (var i = 0; i < that.imglist.length; i++) {
                filelist.push(that.imglist[i].url);
            }
            // var options = {};
            // options.action = 'savethreadpost';
            // options.content = that.form.content;
            // options.CategoryID = that.form.CategoryID;
            // options.CityName = that.form.CityName;
            // options.type = 1;
            ns.post(that.form, function(succeed, data, err) {
                console.log(JSON.stringify(that.form));
                console.log(JSON.stringify(data));
                if (succeed) {
                    that.do_reset();
                    api.sendEvent({
                        name: 'updatabbs',
                    });
                    api.toast({
                        msg: '举报成功',
                        duration: 2000,
                        location: 'bottom'
                    });
                    setTimeout(function() {
                        api.closeWin();
                    }, 1000);
                } else if (err) {
                    api.toast({
                        msg: err,
                        duration: 2000,
                        location: 'bottom'
                    });
                }
            }, {
                files: {
                    file: filelist
                },
                toast: true
            });
        },
        choose_picture: function() {
            var that = this;
            ns.confirmPer('camera', function() {
                that.choose_picture_start();
            })
        },
        choose_picture_start: function() {
            var that = this;
            if (that.imglist.length >= that.maxuploadcount) {
                api.toast({
                    msg: '最多支持' + that.maxuploadcount + '张图片',
                    duration: 2000,
                    location: 'bottom'
                });
                return;
            }
            UIAlbumBrowser.imagePicker({
                max: that.maxuploadcount - that.imglist.length,
                styles: {
                    bg: '#000000',
                    mark: {
                        position: 'top_right',
                        size: 20
                    },
                    nav: {
                        bg: '#000000',
                        cancelColor: '#fff',
                        cancelSize: 16,
                        nextStepColor: '#7fff00',
                        nextStepSize: 16
                    },
                    thumbnail: { //（可选项）返回的缩略图配置，**建议本图片不要设置过大** 若已有缩略图，则使用已有的缩略图。若要重新生成缩略图，可先调用清除缓存接口api.clearCache()。
                        w: 100, //（可选项）数字类型；返回的缩略图的宽；默认：原图的宽度
                        h: 100 //（可选项）数字类型；返回的缩略图的宽；默认：原图的高度
                    }
                },
                animation: true,
            }, function (ret) {
                if (ret.eventType == 'nextStep') {
                    if (ret.list && ret.list.length > 0) {
                        that.select_imglist = ret.list;
                        that.select_imgindex = 0;
                        that.UIAlbumBrowser_transPath();
                    }
                    UIAlbumBrowser.closePicker();
                }
                if (ret.originalPath && ret.originalPath.length > 0) {
                    that.imglist.push({
                        index: that.imgindex,
                        url: ret.originalPath,
                        thumbPath: ret.originalPath
                    });
                    that.imgindex++;
                }
            });
            return;
        },
        UIAlbumBrowser_transPath: function () {
            var that = this;
            var item = that.select_imglist[that.select_imgindex];
            UIAlbumBrowser.transPath({
                path: item.path
            }, function (ret, err) {
                if (ret) {
                    that.select_imgindex++;
                    if (that.select_imgindex < that.select_imglist.length) {
                        that.UIAlbumBrowser_transPath();
                    } else {
                        that.select_imgindex = 0;
                    }
                    that.imglist.push({
                        index: that.imgindex,
                        url: ret.path,
                        thumbPath: item.thumbPath
                    });
                    that.imgindex++;
                }
            });
        },
        remove_img:function(index){
            var that = this;
            for (var i = 0; i < that.imglist.length; i++) {
                var item = that.imglist[i];
                if (item.index == index) {
                    that.imglist.splice(i, 1);
                }
            }

        },
        do_viewimages:function(activeIndex){
            var that = this;
            var imageUrls = [];
            for (var i = 0; i < that.imglist.length; i++) {
                imageUrls.push(that.imglist[i].url);
            }
            var imageBrowser = api.require('imageBrowser');
            imageBrowser.openImages({
                showList: false,
                activeIndex: activeIndex,
                imageUrls: imageUrls
            });
        },
        insert_content: function(index, type, content) {
            var that = this;
            var item = null;
            if (type == 1) {
                item = {
                    id: 0,
                    content: '',
                    type: 'text'
                };
            } else if (type == 2) {
                item = {
                    id: 0,
                    content: content,
                    type: 'image'
                };
            }
            if (index == -1) {
                that.list.push(item);
                return;
            }
            var newlist = [];
            for (var i = 0; i < that.list.length; i++) {
                var current = that.list[i];
                if (i < index) {
                    newlist.push((current));
                }
                if (i == index) {
                    newlist.push(current);
                    newlist.push(item);
                }
                if (i > index) {
                    newlist.push(current);
                }
            }
            that.list = newlist;
        },
        do_remove: function(item, index) {
            var that = this;
            api.confirm({
                title: '提示',
                msg: '确定删除?',
                buttons: ['确定', '取消']
            }, function(ret, err) {
                if (ret.buttonIndex == 1) {
                    that.list.splice(index, 1);
                    if (item.id > 0) {
                        that.remove_server_content(item.id);
                    }
                }
            });
        },
        do_upper: function(index) {
            var that = this;
            var newlist = [];
            for (var i = 0; i < that.list.length; i++) {
                var current = that.list[i];
                if (i < index - 1) {
                    newlist.push((current));
                }
                if (i == index - 1) {
                    newlist.push(that.list[index]);
                    newlist.push(current);
                }
                if (i > index) {
                    newlist.push(current);
                }
            }
            that.list = newlist;
        },
    }
});
apiready = function() {
    api.parseTapmode();
    map = api.require('bMap');
    ns = window.Foresight.Util;
    actionsheet = new auiActionsheet();
    UIAlbumBrowser = api.require('UIAlbumBrowser');
    toast = new auiToast();
    app.form.ReportedUserID = api.pageParam.ReportedUserID;
    app.form.ThreadID = api.pageParam.ThreadID;
    app.form.ProjectID = api.pageParam.PrijectID;
    app.form.ReportType = api.pageParam.type;
    app.form.Type=api.pageParam.postType;
    app.get_data();
};
